class_name GrowthCycleComponent
extends Node

@export var current_growth_state: DataTypes.GrowthStates = DataTypes.GrowthStates.Germination
@export_range(5,365)var days_until_harvest:int=7

signal crop_maturity
signal crop_harverting

var is_watered:bool
var starting_day:int
var current_day:int

func _ready() -> void:
	DayAndNightCycleManager.time_tick_day.connect(on_time_tick_day)

func on_time_tick_day(day:int)->void:
	if is_watered:
		if starting_day==0:
			starting_day=day
		
		growth_states(starting_day,day)
		harvest_state(starting_day,day)

func growth_states(start_day:int, curr_day:int):
	if current_growth_state ==DataTypes.GrowthStates.Maturity:
		return
		
	var num_states =5
	var growth_days_passed =(curr_day-start_day)%num_states
	var state_index =growth_days_passed%num_states+1
	
	current_growth_state=state_index as DataTypes.GrowthStates
	
	var state_name =DataTypes.GrowthStates.keys()[current_growth_state]
	print("Current growth state: ",state_name," State index: ",state_index)
	
	if current_growth_state == DataTypes.GrowthStates.Maturity:
		crop_maturity.emit()
	
func harvest_state(start_day:int, curr_day:int)->void:
	if current_growth_state ==DataTypes.GrowthStates.Harvesting:
		return

	var day_passed =(curr_day - start_day)%days_until_harvest
	
	if day_passed == days_until_harvest -1:
		current_growth_state = DataTypes.GrowthStates.Harvesting
		crop_harverting.emit()

func get_current_growth_state()->DataTypes.GrowthStates:
	return current_growth_state
